//
//  DetailViewController.m
//  YouWen
//
//  Created by Lee GJ on 15-3-10.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 60)];
    self.webView.backgroundColor = [UIColor redColor];
    //自动对页面进行缩放以适应屏幕
    self.webView.scalesPageToFit = YES;
    
    
    [(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setBounces:NO];
    
    // 指定代理人
    self.webView.delegate = self;
    
    [self.view addSubview:self.webView];
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@", self.model.newsLink];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    //设置返回按钮
    UIImage *buttonImage = [UIImage imageNamed:@"返回(1).png"];
    //按钮图片取消渲染
    buttonImage = [buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:buttonImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
    
    //收藏按钮(点击收藏)
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *collectImage = [UIImage imageNamed:@"star@2x.png"];
    [self.collectButton setBackgroundImage:collectImage forState:UIControlStateNormal];
    self.collectButton.frame = CGRectMake(0, 0, 30, 30);
    [self.collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    //收藏按钮(点击取消收藏)
    self.collectButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *collectImage2 = [UIImage imageNamed:@"star_selected@2x.png"];
    [self.collectButton2 setBackgroundImage:collectImage2 forState:UIControlStateNormal];
    self.collectButton2.frame = CGRectMake(0, 0, 30, 30);
    [self.collectButton2 addTarget:self action:@selector(collectButton2Action:) forControlEvents:UIControlEventTouchUpInside];
    
    //判断 根据是否收藏 决定显示哪个button
    if (self.isCollect == NO) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectButton];
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectButton2];
    }
    

}
//左按钮方法
- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//收藏按钮 方法
- (void)collectButtonAction:(UIButton *)button
{
    //代理人执行代理方法
    [self.myDelegate cellect];
    
    //收藏之后弹出UIAlterView
    self.alertView = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [self.webView addSubview:self.alertView];
    
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
    [self.alertView show];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectButton2];
}

- (void)timerAction
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    [self.alertView2 dismissWithClickedButtonIndex:0 animated:YES];
}
//取消收藏按钮 方法
- (void)collectButton2Action:(UIButton *)button
{
    //代理人执行代理方法
    [self.myDelegate2 deleteCar];
    
    self.alertView2 = [[UIAlertView alloc]initWithTitle:@"取消收藏" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [self.webView addSubview:self.alertView2];
    [self.alertView2 show];
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectButton];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
